package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.Chip;
import javafx.scene.image.Image;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;
import xworker.javafx.util.JavaFXUtils;

public class ChipActions {
    static{
        PropertyFactory.regist(Chip.class, "deletableProperty", o -> {
            Chip obj = (Chip) o;
            return obj.deletableProperty();
        });
        PropertyFactory.regist(Chip.class, "textProperty", o -> {
            Chip obj = (Chip) o;
            return obj.textProperty();
        });
        PropertyFactory.regist(Chip.class, "onDeleteActionProperty", o -> {
            Chip obj = (Chip) o;
            return obj.onDeleteActionProperty();
        });
    }

    public static void init(Chip node, Thing thing, ActionContext actionContext){
        ControlActions.init(node, thing, actionContext);

        if(thing.valueExists("deletable")){
            node.setDeletable(thing.getBoolean("deletable"));
        }

        Image graphic = JavaFXUtils.getImage(thing, "graphic", actionContext);
        if(graphic != null){
            node.setGraphic(graphic);
        }

        String text = JavaFXUtils.getString(thing, "text", actionContext);
        if(text != null){
            node.setText(text);
        }
    }

    public static Chip create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Chip node = new Chip();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }
}
